package com.imay.live.fragment.room;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.R;

/**
 * Created by jeaman on 2017/6/7.
 */

public class BeautyFragment extends BaseDialogFragment implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
    private final static String BUNDLE_NAME_BEAUTY = "beauty";
    private final static String BUNDLE_NAME_WHITENING = "whitening";
    private final static String BUNDLE_NAME_RUDDY = "ruddy";

    //素颜
    private final static int BEAUTY_NO = 0;
    private final static int WHITENING_NO = 0;
    private final static int RUDDY_NO = 0;
    //俊男
    private final static int BEAUTY_BOY = 5;
    private final static int WHITENING_BOY = 6;
    private final static int RUDDY_BOY = 6;
    //美人
    private final static int BEAUTY_GIRL = 8;
    private final static int WHITENING_GIRL = 7;
    private final static int RUDDY_GIRL = 7;

    private SeekBar mBeautySb;
    private SeekBar mWhiteningSb;
    private SeekBar mRuddySb;
    private View mNoView;
    private View mBoyView;
    private View mGirlView;

    public static BeautyFragment newInstance(int beauty, int whitening, int ruddy) {
        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_NAME_BEAUTY, beauty);
        bundle.putInt(BUNDLE_NAME_WHITENING, whitening);
        bundle.putInt(BUNDLE_NAME_RUDDY, ruddy);
        BeautyFragment fragment = new BeautyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_beauty;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mBeautySb = $(R.id.beauty_sb_beauty);
        mWhiteningSb = $(R.id.beauty_sb_whitening);
        mRuddySb = $(R.id.beauty_sb_ruddy);
        mNoView = $(R.id.beauty_tv_no);
        mBoyView = $(R.id.beauty_tv_boy);
        mGirlView = $(R.id.beauty_tv_girl);
        initListener();

        if (getArguments() != null) {
            int beauty = getArguments().getInt(BUNDLE_NAME_BEAUTY);
            int whitening = getArguments().getInt(BUNDLE_NAME_WHITENING);
            int ruddy = getArguments().getInt(BUNDLE_NAME_RUDDY);
            setBeautyValue(beauty, whitening, ruddy);
        }
    }

    @Override
    protected int getWindowAnimations() {
        return R.style.dialog_from_bottom;
    }

    @Override
    protected boolean isMatchParent() {
        return true;
    }

    @Override
    protected int getHeight() {
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected boolean isTranslucentBackground() {
        return true;
    }

    @Override
    protected boolean isOutsideTouchable() {
        return true;
    }

    private void initListener() {
        mNoView.setOnClickListener(this);
        mBoyView.setOnClickListener(this);
        mGirlView.setOnClickListener(this);
        mBeautySb.setOnSeekBarChangeListener(this);
        mWhiteningSb.setOnSeekBarChangeListener(this);
        mRuddySb.setOnSeekBarChangeListener(this);
    }

    private void setBeautyValue(int beauty, int whitening, int ruddy) {
        mBeautySb.setProgress(beauty);
        mWhiteningSb.setProgress(whitening);
        mRuddySb.setProgress(ruddy);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (getActivity() instanceof OnBeautyChangeListener) {
            ((OnBeautyChangeListener) getActivity()).onBeautyDismiss();
        }
        super.onDismiss(dialog);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.beauty_tv_no:
                setBeautyValue(BEAUTY_NO, WHITENING_NO, RUDDY_NO);
                break;
            case R.id.beauty_tv_boy:
                setBeautyValue(BEAUTY_BOY, WHITENING_BOY, RUDDY_BOY);
                break;
            case R.id.beauty_tv_girl:
                setBeautyValue(BEAUTY_GIRL, WHITENING_GIRL, RUDDY_GIRL);
                break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        int beautyLevel = mBeautySb.getProgress();
        int whiteningLevel = mWhiteningSb.getProgress();
        int ruddyLevel = mRuddySb.getProgress();
        mNoView.setSelected(beautyLevel == BEAUTY_NO && whiteningLevel == WHITENING_NO && ruddyLevel == RUDDY_NO);
        mBoyView.setSelected(beautyLevel == BEAUTY_BOY && whiteningLevel == WHITENING_BOY && ruddyLevel == RUDDY_BOY);
        mGirlView.setSelected(beautyLevel == BEAUTY_GIRL && whiteningLevel == WHITENING_GIRL && ruddyLevel == RUDDY_GIRL);


        if (getActivity() instanceof OnBeautyChangeListener) {
            ((OnBeautyChangeListener) getActivity()).onBeautyChange(beautyLevel, whiteningLevel, ruddyLevel);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    public interface OnBeautyChangeListener {
        void onBeautyChange(int beautyLevel, int whiteningLevel, int ruddyLevel);
        void onBeautyDismiss();
    }
}
